package com.cby.operation.common.util

import android.text.TextUtils
import java.util.regex.Pattern

/**
 * Description:StringUtils
 *
 * @author 陈宝阳
 * @create 2020/9/2 13:48
 */
object StringUtils {
    private const val HTTP_URL_REGEX = "^((ht|f)tps?):\\/\\/([\\w\\-]+(\\.[\\w\\-]+)*\\/)*[\\w\\-]+(\\.[\\w\\-]+)*\\/?(\\?([\\w\\-\\.,@?^=%&:\\/~\\+#]*)+)?"


    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @return boolean
     */
    fun isEmpty(input: String?): Boolean {
        if (input == null || "" == input) {
            return true
        }
        for (element in input) {
            if (element != ' ' && element != '\t' && element != '\r' && element != '\n') {
                return false
            }
        }
        return true
    }

    /**
     * 校验url是否是合法的地址
     * @param url
     * @return
     */
    fun isValidHttpUrl(url: String?): Boolean {
        if (TextUtils.isEmpty(url)) {
            return false
        }
        val pattern = Pattern.compile(HTTP_URL_REGEX)
        return pattern.matcher(url).matches()
    }
}